package com.shuzi.MediaShare.base;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.shuzi.MediaShare.R;
import com.shuzi.MediaShare.download.DownloadManager;
import com.shuzi.MediaShare.view.TitileBarLayout;
import com.shuzi.MediaShare.view.TitileBarLayout.OnTitleBarClick;

/**
 * 基础activity框架抽象类
 * 
 * @author lung 下午9:09:42 2013
 */
public abstract class BaseFrameAtivity extends Activity
{

	/** 加载等待视图 */
	protected RelativeLayout loadingView = null;

	/** 错误时显示的视图 */
	protected RelativeLayout errorView = null;

	/** 中间视图 */
	private RelativeLayout centerViewLayout = null;

	/** 标题栏 */
	protected TitileBarLayout titileBarLayout = null;

	/** 重试按钮 */
	private Button tryAgainButton = null;

	/** 遇到错误后是否显示错误视图 */
	protected boolean isShowErrorView = true;

	/** 是否已经销毁 */
	private boolean isDestory = false;

	@Override
	protected void onDestroy()
	{
		isDestory = true;
		super.onDestroy();
	}

	public void setShowErrorView(boolean isShowErrorView)
	{
		this.isShowErrorView = isShowErrorView;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		initView();
	}

	/**
	 * 初始化视图
	 */
	private void initView()
	{
		setContentView(R.layout.base_main);
		loadingView = (RelativeLayout) findViewById(R.id.loadingView);
		centerViewLayout = (RelativeLayout) findViewById(R.id.centerView);
		errorView = (RelativeLayout) findViewById(R.id.errorView);
		titileBarLayout = (TitileBarLayout) findViewById(R.id.main_titlebar);
		centerViewLayout.addView(setCenterView(),
				new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
						LayoutParams.FILL_PARENT));
		titileBarLayout.setOnTitleClick(new OnTitleBarClick()
		{

			@Override
			public void onClick(View view)
			{
				if (view.getId() != R.id.choice_button)
				{
					finish();
				} else
				{
					showTypeChoice();
				}

			}

		});
		tryAgainButton = (Button) findViewById(R.id.tryAgain);
		tryAgainButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (DownloadManager.getInstace().isNetWorksAvailable)
				{
					if (errorView != null)
					{
						errorView.setVisibility(View.GONE);
					}
					loadingView.setVisibility(View.VISIBLE);
					tryAgain();
				} else
				{
					Toast.makeText(BaseFrameAtivity.this, "无网络，请设置网络",
							Toast.LENGTH_SHORT).show();
				}

			}
		});
	}

	/**
	 * 设置标题栏是否显示
	 * 
	 * @param visible
	 */
	public void setTitleBarVisible(boolean visible)
	{
		if (titileBarLayout != null)
		{
			if (visible)
			{
				titileBarLayout.setVisibility(View.VISIBLE);
			} else
			{
				titileBarLayout.setVisibility(View.GONE);
			}
		}
	}

	/**
	 * 
	 * 加载数据
	 * 
	 * @param params
	 */
	public void loadData(Integer... params)
	{

		if (!DownloadManager.getInstace().isNetWorksAvailable)
		{
			loadingView.setVisibility(View.GONE);
			errorView.setVisibility(View.VISIBLE);
			Toast.makeText(BaseFrameAtivity.this, "无网络，请设置网络",
					Toast.LENGTH_SHORT).show();
			return;
		}

		new AsyncTask<Integer, Void, Boolean>()
		{

			Integer[] paIntegers;

			@Override
			protected Boolean doInBackground(Integer... params)
			{
				this.paIntegers = params;
				return initData(params);
			}

			@Override
			protected void onPostExecute(Boolean result)
			{
				super.onPostExecute(result);
				loadingView.setVisibility(View.GONE);
//				if (!result && isShowErrorView)
//				{
//					errorView.setVisibility(View.VISIBLE);
//				}
				if (!isDestory)
				{
					refreshView(result, paIntegers);
				}

			}

		}.execute(params);
	}

	/**
	 * 
	 * 耗时操作获取数据
	 * 
	 * @param params
	 * @return
	 */
	protected abstract boolean initData(Integer... params);

	/**
	 * 获取数据完成后刷新视图
	 * 
	 * @param params
	 * @param result
	 *            是否成功
	 * @return
	 */
	protected abstract void refreshView(boolean isSuccessful, Integer... params);

	/**
	 * 重试
	 */
	protected abstract void tryAgain();

	/**
	 * 子类中间视图
	 * 
	 * @return
	 */
	protected abstract View setCenterView();

	/**
	 * 设置筛选的视图
	 */
	protected void showTypeChoice()
	{

	}
	
	/**
	 * 设置全屏
	 */
	protected void setFullScreen(){
	     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}
	
	/**
	 * 退出全屏
	 */
	protected void quitFullScreen(){
	      final WindowManager.LayoutParams attrs = getWindow().getAttributes();
	      attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
	      getWindow().setAttributes(attrs);
	      getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
	}


}
